package hashtable;

import java.util.Scanner;

public class HashTabDemo {
    public static void main(String[] args) {
        HashTab hashTab = new HashTab(5);
        Scanner scanner = new Scanner(System.in);
        String key = "";
        while (true) {
            System.out.println("add:  添加雇员");
            System.out.println("list: 列出雇员");
            System.out.println("find: 查找雇员");
            System.out.println("exit: 退出系统");
            key = scanner.next();
            switch (key) {
                case "add":
                    System.out.println("请输入员工id>");
                    int id = scanner.nextInt();
                    System.out.println("输入名字>");
                    String name = scanner.next();
                    Emp emp = new Emp(id, name);
                    hashTab.add(emp);
                    break;
                case "list":
                    hashTab.list();
                    break;
                case "find":
                    System.out.println("请输入查找id>");
                    id = scanner.nextInt();
                    hashTab.finEmpById(id);
                    break;
                case "delete":
                    System.out.println("请输入要删除的id>");
                    id = scanner.nextInt();
                    hashTab.deleteEmpById(id);
                    break;
                case "exit":
                    scanner.close();
                    System.exit(0);
                    break;
                default:
                    break;
            }
        }

    }
}

class HashTab {
    private EmpLinkedList[] empLinkedListArray;
    private int size;

    public HashTab(int size) {
        this.size = size;
        empLinkedListArray = new EmpLinkedList[size];
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i] = new EmpLinkedList();
        }
    }

    public void add(Emp emp) {
        int empLinkedListNo = hashFun(emp.id);
        empLinkedListArray[empLinkedListNo].add(emp);
    }

    public void list() {
        for (int i = 0; i < size; i++) {
            empLinkedListArray[i].list(i);
        }
    }

    public void finEmpById(int id) {
        int empLinkedListNo = hashFun(id);
        Emp emp = empLinkedListArray[empLinkedListNo].findEmpById(id);
        if (emp != null) {
            System.out.printf("在第%d条链表中找到 雇员 id=%d\n", (empLinkedListNo + 1), id);
        } else {
            System.out.println("没有找到该雇员~");
        }
    }
    public void deleteEmpById(int id) {
        int emLinkedListNo = hashFun(id);
        empLinkedListArray[emLinkedListNo].deleteEmpById(id);
    }


    public int hashFun(int id) {
        return id % size;
    }
}


class EmpLinkedList {
    private Emp head;

    public void add(Emp emp) {
        if (head == null) {
            head = emp;
            return;
        }
        Emp curEmp = head;
        while (curEmp.next != null) {
            curEmp = curEmp.next;
        }
        curEmp.next = emp;
    }

    public void list(int no) {
        if (head == null) {
            System.out.println("第 " + (no + 1) + " 链表为空");
            return;
        }
        System.out.print("第 " + (no + 1) + " 链表信息为");
        Emp curEmp = head;
        while (true) {
            System.out.printf(" -> id=%d name=%s\t", curEmp.id, curEmp.name);
            if (curEmp.next == null) {
                break;
            }
            curEmp = curEmp.next;
        }
        System.out.println();
    }

    public Emp findEmpById(int id) {
        if (head == null) {
            System.out.println("链表为空~");
            return null;
        }
        Emp curEmp = head;
        while (true) {
            if (curEmp.id == id) {
                break;
            }
            if (curEmp.next == null) {
                curEmp = null;
                break;
            }
            curEmp = curEmp.next;
        }
        return curEmp;
    }

    public void deleteEmpById(int id){
        if (head == null) {
            System.out.println("链表为空~");
            return;
        }
        if(head.id == id){
            head = head.next;
            return;
        }
        Emp curEmp = head;
        while(curEmp.next != null && curEmp.next.id != id){
            curEmp = curEmp.next;
        }
        if(curEmp.next == null){
            System.out.println("没找到待删除节点~");
            return;
        }
        curEmp.next = curEmp.next.next;
    }
}

class Emp {
    Emp(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int id;
    public String name;
    public Emp next;
}
